<?php 
    namespace Media\Repository;
    
    class AlbumRepository extends AbstractRepository {
        protected  $entity="Media\\Entity\\Album";
        
        public function listLimit($uid, $number=null){
            $dql = "SELECT c FROM $this->entity c JOIN c.user_uid u WHERE u.uid = ?1 order by c.id desc";
            $query = $this->getEntityManager()->createQuery($dql);
            $query->setParameter(1, $uid);
            if($number!=null && $number>0) $query->setMaxResults($number);
            return $query->getResult();
        }
        
        public function listFromMin($uid, $minId, $maxResult=null, $excludeMin=true){
            $quantifier = $excludeMin?">":">=";
            $dql = "SELECT c FROM $this->entity c JOIN c.user_uid u WHERE u.uid = ?2 and c.id ".$quantifier." ?1 order by c.id asc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $minId)
                ->setParameter(2, $uid);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listFromMax($uid, $maxId, $maxResult=null, $excludeMax=true){
            $quantifier = $excludeMax?"<":"<=";
            $dql = "SELECT c FROM $this->entity c JOIN c.user_uid u WHERE u.uid = ?2 and c.id ".$quantifier." ?1 order by c.id desc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $maxId)
                ->setParameter(2, $uid);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listByUser($idUser, $maxResult=null){
            $dql = "SELECT c FROM $this->entity c JOIN c.user_uid u WHERE u.uid = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idUser);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function getByProvider($idUser, $providerId){
            $dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.user_uid) = ?1 AND IDENTITY(c.provider_id) = ?2";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idUser)
                ->setParameter(2, $providerId);
            $query->setMaxResults(1);
            $results = $query->getResult();
            return ($results==null||count($results)==0)?null:$results[0];
        }
        
        
        public function listFacebookAlbums($idUser, $facebook_ids_array){
            if($facebook_ids_array==null || count($facebook_ids_array)==0) return array();
            $dql = "SELECT c FROM $this->entity c WHERE IDENTITY(c.user_uid) = ?1 AND c.source_album_facebook_id IN (?2)";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idUser)
                ->setParameter(2, $facebook_ids_array);
            return $query->getResult();
        }
        
        
    }
